{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为____None____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为___[30,4]_____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为____5___."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为__97______."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(  错  )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`( 错   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`( 错   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`( 对   )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串，输出其中每个字符的次数:2346\n",
      "Counter({'2': 1, '3': 1, '4': 1, '6': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "text = input('请输入一个字符串，输出其中每个字符的次数:')\n",
    "print(Counter(text))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串，输出其中只出现了一次的字符及其下标:35468\n",
      "[('3', 0), ('5', 1), ('4', 2), ('6', 3), ('8', 4)]\n"
     ]
    }
   ],
   "source": [
    "text = input('请输入一个字符串，输出其中只出现了一次的字符及其下标:')\n",
    "positions = [(ch,index) for index,ch in enumerate(text) if text.index(ch) == text.rindex(ch)]\n",
    "print(positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串:4678\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "x = input('请输入一个字符串:')\n",
    "d = {}\n",
    "for i in x:\n",
    "     d[i] = d.get(i,0)+1\n",
    "for j in d:\n",
    "    if d[j] == 1:\n",
    "        print(x.index(j))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:[58,9,6,3]\n",
      "63\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "lstA = eval(input('请输入一个列表:'))\n",
    "print(reduce(lambda x,y:x | y,lstA))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串23432\n",
      "Yes\n"
     ]
    }
   ],
   "source": [
    "str1 = input('请输入一个字符串')\n",
    "str2 = str1[::-1]\n",
    "if str1==str2:\n",
    "    print('Yes')\n",
    "else:\n",
    "    print('No')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、可迭代对象\n",
    "2、True\n",
    "3、None\n",
    "4、remove\n",
    "5、[6,7,9,11,13]\n",
    "6、{} 、键 、值 、键\n",
    "7、items()、keys()、values()\n",
    "8、dict(zip(a,b))\n",
    "9、b=print(a[::3])\n",
    "10、[5 fot i in range(10)]\n",
    "11、不可以\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12、"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['sfcgtrtj46680', '34fsr', '68hff', '0.2']\n"
     ]
    }
   ],
   "source": [
    "data = ['sfcgtrtj46680','34fsr','68hff','dd','0.2']\n",
    "print(list(filter(lambda s:len(set(s))/len(s)>0.5,data)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13、"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def prime(x):\n",
    "    for i in range(2,x):\n",
    "        if x%i==0:\n",
    "            return False\n",
    "        if i==x-1:\n",
    "            return True\n",
    "output=filter(prime,range(2,1001))\n",
    "print(list(output))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14题、速度更快"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15题、"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[68, 96, 75, 27, 84, 58, 75, 58, 62, 39, 10, 42, 72, 79, 42, 70, 39, 23, 100, 95, 6, 27, 89, 64, 57, 55, 16, 35, 95, 10, 21, 1, 94, 17, 3, 4, 14, 100, 91, 20, 4, 25, 86, 52, 34, 43, 26, 54, 75, 81, 84, 94, 44, 47, 53, 57, 12, 52, 16, 27, 64, 60, 25, 62, 95, 91, 34, 64, 79, 65, 66, 56, 99, 17, 25, 88, 18, 92, 14, 95, 71, 94, 87, 96, 4, 20, 1, 88, 42, 65, 76, 41, 74, 77, 64, 77, 71, 62, 23, 59]\n",
      "68 : 1\n",
      "96 : 2\n",
      "75 : 3\n",
      "27 : 3\n",
      "84 : 2\n",
      "58 : 2\n",
      "75 : 3\n",
      "58 : 2\n",
      "62 : 3\n",
      "39 : 2\n",
      "10 : 2\n",
      "42 : 3\n",
      "72 : 1\n",
      "79 : 2\n",
      "42 : 3\n",
      "70 : 1\n",
      "39 : 2\n",
      "23 : 2\n",
      "100 : 2\n",
      "95 : 4\n",
      "6 : 1\n",
      "27 : 3\n",
      "89 : 1\n",
      "64 : 4\n",
      "57 : 2\n",
      "55 : 1\n",
      "16 : 2\n",
      "35 : 1\n",
      "95 : 4\n",
      "10 : 2\n",
      "21 : 1\n",
      "1 : 2\n",
      "94 : 3\n",
      "17 : 2\n",
      "3 : 1\n",
      "4 : 3\n",
      "14 : 2\n",
      "100 : 2\n",
      "91 : 2\n",
      "20 : 2\n",
      "4 : 3\n",
      "25 : 3\n",
      "86 : 1\n",
      "52 : 2\n",
      "34 : 2\n",
      "43 : 1\n",
      "26 : 1\n",
      "54 : 1\n",
      "75 : 3\n",
      "81 : 1\n",
      "84 : 2\n",
      "94 : 3\n",
      "44 : 1\n",
      "47 : 1\n",
      "53 : 1\n",
      "57 : 2\n",
      "12 : 1\n",
      "52 : 2\n",
      "16 : 2\n",
      "27 : 3\n",
      "64 : 4\n",
      "60 : 1\n",
      "25 : 3\n",
      "62 : 3\n",
      "95 : 4\n",
      "91 : 2\n",
      "34 : 2\n",
      "64 : 4\n",
      "79 : 2\n",
      "65 : 2\n",
      "66 : 1\n",
      "56 : 1\n",
      "99 : 1\n",
      "17 : 2\n",
      "25 : 3\n",
      "88 : 2\n",
      "18 : 1\n",
      "92 : 1\n",
      "14 : 2\n",
      "95 : 4\n",
      "71 : 2\n",
      "94 : 3\n",
      "87 : 1\n",
      "96 : 2\n",
      "4 : 3\n",
      "20 : 2\n",
      "1 : 2\n",
      "88 : 2\n",
      "42 : 3\n",
      "65 : 2\n",
      "76 : 1\n",
      "41 : 1\n",
      "74 : 1\n",
      "77 : 2\n",
      "64 : 4\n",
      "77 : 2\n",
      "71 : 2\n",
      "62 : 3\n",
      "23 : 2\n",
      "59 : 1\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x = [random.randint(0,100)for i in range(100)]\n",
    "print(x)\n",
    "for i in x:\n",
    "    print(i,':',x.count(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16、"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:[1,2,3,4,5,6]\n",
      "请输入第一个下标:2\n",
      "请输入第二个下标:5\n",
      "[3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "list = eval(input('请输入一个列表:'))\n",
    "a = eval(input('请输入第一个下标:'))\n",
    "b = eval(input('请输入第二个下标:'))\n",
    "if a>b:\n",
    "    for i in range(b,a):\n",
    "        print(list[b:a])\n",
    "        break\n",
    "elif a<b:\n",
    "    for i in range(a,b):\n",
    "        print(list[a:b])\n",
    "        break\n",
    "elif a==b:\n",
    "    print([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input the name:Wusi\n",
      "您输入的键不存在!\n"
     ]
    }
   ],
   "source": [
    "d={'Liming':12,'Zhangsan':13}\n",
    "a=input('input the name:')\n",
    "print(d.get(a,'您输入的键不存在!'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[67, 98, 11, 20, 87, 29, 50, 6, 90, 52, 6, 9, 87, 20, 63, 5, 84, 54, 75, 68]\n",
      "[6, 11, 20, 29, 50, 52, 67, 87, 90, 98, 87, 84, 75, 68, 63, 54, 20, 9, 6, 5]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x = [random.randint(0,100) for i in range(20)]\n",
    "print(x) \n",
    "x1=x[0:10]\n",
    "x1.sort()\n",
    "x[0:10]=x1\n",
    "x1=x[10:20]\n",
    "x1.sort(reverse=True)\n",
    "x[10:20]=x1\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
